# encoding: utf-8
import datetime
import json
import os
import random
import time

import requests
import xlwt

url = 'https://weibo.com/ajax/statuses/mymblog'
uid = 0
data = {}
# cookie池
cookies = []
year_month_pool = []
# 请求头
headers = {
    'referer': 'https://weibo.com/cctvxinwen'
}
# 停顿步长
step = 20


def get_year_month_pool():
    year_month_list = year_month_group.split(',')
    for year_month in year_month_list:
        year = year_month.split('-')[0]
        month = year_month.split('-')[1]
        year_month_pool.append({'displayYear': year, "curMonth": month, 'stat_date': year + month})


def init_params():
    data['page'] = page
    headers['cookie'] = random.choice(cookies)


def sheet_header(sheet):
    sheet.write(0, 0, "发布时间")
    sheet.write(0, 1, "发布来源")
    sheet.write(0, 2, "发布内容")
    sheet.write(0, 3, "转发数")
    sheet.write(0, 4, "评论数")
    sheet.write(0, 5, "点赞数")
    sheet.write(0, 6, "详细链接")


if __name__ == '__main__':
    # 从读取用户输入的配置参数
    uid = input("请输入要抓取的uid:")
    year_month_group = input('请输入要抓取的年月，用","分割：')
    get_year_month_pool()
    print('用"｜coderwe｜"分割，如A｜coderwe｜B，即为A、B两个cookie。')
    cookie_group = input('请输入cookie组：')
    cookies = cookie_group.split('|coderwe|')
    file_name = input("请输入需要导出xls的文件名:")
    # 开始下载数据
    print('开始抓取数据')
    for year_month in year_month_pool:
        with open(year_month['stat_date'] + '.json', 'w', encoding='utf-8') as f:
            page = 1
            data = year_month
            data['feature'] = 0
            data['uid'] = uid
            while True:
                if page % step == 0:
                    print('反反爬机制，暂停10秒')
                    time.sleep(10)
                init_params()
                response = requests.get(url, params=data, headers=headers).json()
                reports = response['data']['list']
                if len(reports) == 0:
                    break
                for report in reports:
                    content = {
                        'date': datetime.datetime.strptime(report['created_at'], '%a %b %d %H:%M:%S %z %Y').strftime(
                            '%Y-%m-%d %H:%M:%S'),
                        'source': report['source'],
                        'text_raw': report['text_raw'],
                        'reposts': report['reposts_count'],
                        'comment': report['comments_count'],
                        'attitudes': report['attitudes_count'],
                        'detail_url': 'https://weibo.com/' + uid + '/' + report['mblogid']
                    }
                    f.writelines(json.dumps(content, ensure_ascii=False) + "\n")
                page = page + 1
    print('数据已全部写入临时json文件')
    # 将数据导出到xml中
    print('开始导出数据到xml中')
    book = xlwt.Workbook('utf-8', 0)
    for year_month in year_month_pool:
        with open(year_month['stat_date'] + '.json', 'r', encoding='utf-8') as f:
            sheet = book.add_sheet(year_month['stat_date'], True)
            sheet_header(sheet)
            index = 1
            while True:
                report_json = f.readline()
                # 读取完，循环结束
                if len(report_json) == 0:
                    break
                report = json.loads(report_json)
                sheet.write(index, 0, report['date'])
                sheet.write(index, 1, report['source'])
                sheet.write(index, 2, report['text_raw'])
                sheet.write(index, 3, report['reposts'])
                sheet.write(index, 4, report['comment'])
                sheet.write(index, 5, report['attitudes'])
                sheet.write(index, 6, report['detail_url'])
                index = index + 1
    book.save(f'{file_name}.xls')
    # 删除临时缓存的json文件
    print('删除临时文件')
    for year_month in year_month_pool:
        os.remove(year_month['stat_date'] + '.json')
    input('按回车键结束程序')
